package com.pj.wisdomlogistics.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.mvp.contract.RegistContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.RegistPresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;
import com.pj.wisdomlogistics.utils.CountDownTimerUtils;
import com.pj.wisdomlogistics.utils.CustomToast;

import org.greenrobot.eventbus.EventBus;

import br.com.simplepass.loading_button_lib.customViews.CircularProgressButton;
import br.com.simplepass.loading_button_lib.interfaces.OnAnimationEndListener;
import butterknife.BindView;
import butterknife.OnClick;

public class RegisterActivity extends MvpActivity<RegistPresenter> implements RegistContract.View {

    @BindView(R.id.et_name)
    EditText mEtName;
    @BindView(R.id.et_password)
    EditText mEtPassword;
    @BindView(R.id.et_ret_password)
    EditText mEtRetPassword;
    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_code)
    EditText mEtCode;
    @BindView(R.id.ll_code)
    LinearLayout mLlCode;
    @BindView(R.id.tv_getcode)
    TextView mTvCode;
    @BindView(R.id.bt_regist)
    CircularProgressButton mBtRegist;

    private String msg_code;

    private String phone;
    private String password;

    @Override
    public void initView() {
        ultimateBar.setImmersionBar();
    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_register;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }

    @Override
    public void onError(String errorMsg) {
        dismissProgressDialog();
        CustomToast.showToast(this, errorMsg, 1000);

    }

    @Override
    public void onRegistError(String errorMsg) {
        dismissProgressDialog();
        mBtRegist.revertAnimation(new OnAnimationEndListener() {
            @Override
            public void onAnimationEnd() {
                mBtRegist.setText("失败请重试");
            }
        });

        CustomToast.showToast(this, errorMsg, 1000);
    }

    @Override
    public void onCodeSuccess(String code) {
        msg_code = code;
    }

    @Override
    public void onRegistSuccess() {
        dismissProgressDialog();
        EventBus.getDefault().post(new EventCenter<>(Cons.Other.REGIST,  phone + "," + password));
        ActivityManagerUtil.create().finishActivity();
    }

    @Override
    protected BaseView getBaseView() {
        return this;
    }


    @OnClick({R.id.bt_regist, R.id.tv_protocol , R.id.tv_getcode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_regist:
                String name = mEtName.getText().toString().trim();
                password = mEtPassword.getText().toString().trim();
                String retPwd = mEtRetPassword.getText().toString().trim();
                String code = mEtCode.getText().toString().trim();
                phone= mEtPhone.getText().toString().trim();

                if(TextUtils.isEmpty(name)
                        || TextUtils.isEmpty(password)
                        || TextUtils.isEmpty(retPwd)
                        || TextUtils.isEmpty(code)
                        || TextUtils.isEmpty(phone)){
                    CustomToast.showToast(this , "请将资料填写完整" , 1000);
                    return;
                }

                if(!TextUtils.equals(password , retPwd)){
                    CustomToast.showToast(this , "两次密码输入不一致" , 1000);
                    return;
                }

                if(!TextUtils.equals(code , msg_code)){
                    CustomToast.showToast(this , "验证码输入有误" , 1000);
                    return;
                }

                mBtRegist.startAnimation();
                mPresenter.regist("" , password , name , phone);
                showProgressDialog();

                break;
            case R.id.tv_protocol:
                break;
            case R.id.tv_getcode:
                String phone = mEtPhone.getText().toString().trim();
                if(TextUtils.isEmpty(phone)){
                    CustomToast.showToast(this , "请输入手机号" , 1000);
                    return;
                }

                new CountDownTimerUtils(mTvCode, 60000, 1000).start();

                mPresenter.userMsg(phone);
                break;
        }
    }
}
